 //index的login下的js
function get_img_code(){
    // var img = document.getElementById("getcode");
    var randomnum = Math.random();
    // img.src="/account/getVerifyimg?"+randomnum;
    $('#getcode').attr('src','/account/getVerifyimg?'+randomnum);
  }
function Dologin() {
    var username = $('#username').val();
    var password = $('#password').val();
    var imagecode = $('#imagecode').val();
    if (username == "") {
        $.alert("请输入登录帐号");
        return;
    }
    if (password == "") {
        $.alert("请输入登录密码");
        return;
    }
    // if (imagecode == "") {
    //     $.alert("请输入验证码");
    //     return;
    // }
    $.ajax({
             type: "post",
             async: false,
             url: "/h5/login",
             dataType:'json',
             "data":{
                "usercode":username,
                "pwd":strEnc(username+','+password+','+imagecode,1,2,3),
                'vcode':imagecode
             },
             success:function(msg){
                if(msg.statusCode=="200"){
                  console.log('11111111111111111111');
                    location.href = "/h5/index";
                }else{
                    $.alert(msg.message);
                    get_img_code();
                    $('#imagecode').val('');
                }
             },
             error:function(XMLHttpRequest, textStatus, errorThrown){
                if(XMLHttpRequest.status){
                    location.href = "/severError";
                }
             },
            statusCode : {
                    503: function(xhr, ajaxOptions, thrownError) {
                        $.toast('数据加载超时');
                    }
                }
    });
}

function checkundefined(s){  
  if (s==undefined) {
    s="";
  }
  return s;
}

function addDeviceItems(pageCurrent, pageSize, deviceSn,businessName,btn) { 
          $.ajax({
            url:'/devices/list',
            type:'post',
            data:{pageCurrent:pageCurrent, pageSize:pageSize,name:deviceSn,businessname:businessName,'businessname.operator':businessName},
            dataType:'json',
            success:function(json){
              totalRow=json.totalRow;
              if(totalRow<11){
                            $('.infinite-scroll-preloader').remove();
                          }
               $('.device-list').setList(json,'device','',btn,pageCurrent,'list-container device-list');
              },
                statusCode : {
                    503: function(xhr, ajaxOptions, thrownError) {
                        $.toast('数据加载超时');
                    }
                }
        });
}

function addProtocolItems(pageCurrent, pageSize, protocolName, btn) { 
          $.ajax({
            url:'/userProtocol/list',
            type:'post',
            data:{pageCurrent:pageCurrent, pageSize:pageSize,name:protocolName},
            dataType:'json',
            success:function(json){
              totalRow=json.totalRow;
                          if(totalRow<11){
                            $('.infinite-scroll-preloader').remove();
                          }
               $('.protocol-list').setList(json,'protocol','',btn,pageCurrent,'list-container protocol-list');
              },
                statusCode : {
                    503: function(xhr, ajaxOptions, thrownError) {
                        $.toast('数据加载超时');
                    }
                }
        });
}

function device_edit(){   //设备修改
        ids=[],deviceDelArr=[];
        $('#device-content').addClass('mb30');
        $('#footerEditdevice').css('display','flex');
        $('.device-list .item-content #radioBtn').css('display','flex');
        $('[type="radio"]').get(0).checked=false;
        $('[type="radio"]').removeAttr('checked');
        $('[type="checkbox"]').get(0).checked=false;
        $('[type="checkbox"]').removeAttr("checked");      //未选状态
        $('#footerDeldevice').css('display','none');     //编辑时删除不显示
        $('.device-list .item-content #checkboxBtn').css('display','none');  //编辑时多选不显示
      }

        function allDevice_check(obj){
          if($(obj).get(0).checked == true){
            ids=[];
            $('[type="checkbox"]').prop('checked',true); 
            $('.device-list li').each(function(){
              ids.push($(this).find('#itemId').next().text());
            })         
          }else{
            $('[type="checkbox"]').prop('checked',false);
            ids=[];
          }
          console.log(ids);
          console.log(66666);

      }

      function onDevice_check(obj,item){
        debugger
        var index=$(obj).parent('#checkboxBtn').nextAll('a').find('#itemId').next().text();
        if($(obj).get(0).checked == true){
          ids.push(index); 
          console.log(ids);
          console.log(566566);
          if(ids.length == totalRow){
            $('#allCheck').prop('checked',true);
          }
        }else{
          ids.splice( ids.indexOf(index), 1 );
          if(ids.length != totalRow){
            $('#allCheck').prop('checked',false);
          }
        }
      }

        function device_del(){    //用户私协删除
        ids=[],deviceDelArr=[];
        $('#device-content').addClass('mb30');
        $('#footerDeldevice').css('display','flex');
        $('.device-list .item-content #checkboxBtn').css('display','flex');
        $('[type="radio"]').get(0).checked=false;
        $('[type="radio"]').removeAttr('checked');
        $('[type="checkbox"]').get(0).checked=false;
        $('[type="checkbox"]').removeAttr("checked");     //未选状态
        $('#footerEditdevice').css('display','none');   //删除时编辑不显示
        $('.device-list .item-content #radioBtn').css('display','none');  //删除时单选不显示
      }

      function select_onchang(){
              $('#userproto-kValue').attr('changeValue',true);
              $(".field-save-btn").attr("disabled",false);     //按钮可用
              if(objKtv){
                   $.each(objKtv,function(i,item){
                     if(item.id==$('#userproto-kValue option:selected').val()){
                          $(".tValue").val(item.tname);    
                          $(".kValues").val(item.k);    
                          $(".vValue").val('');
                      }
                  })
              }
          }
       
      function field_add_btn(){
              var company= $('input[name="company"]').val();
              var pattern =/^.{0,15}$/; 
              pattern.test(company)
              var companyBool= pattern.test(company)
             if(companyBool==true || companyBool==null){
                  var kid=$("#userproto-kValue").val();
                  var kValue=$("#userproto-kValue").find("option:selected").text();
                  var kValues=$(".kValues").val(); //k值
                  var tValue=$(".tValue").val();
                  var vValue=$(".vValue").val();
                  add_field(kid,kValue,kValues,tValue,vValue)
             }else{
                        $.toast("长度不得超过15个字节");
             }
      }

      function add_field(kid,kname,kValues,t,t_description){
        $(".protocol-list-ul").append("<li>"+
            "<div style='float:left; display:none'  class='display-none kid'>"+kid+"</div>"+
            "<div class='liItem-kname'>"+kname+"</div>"+
            "<div class='liItem-t' >"+t+"</div>"+
            "<div  class='kValues'>"+kValues+"</div>"+
            "<div class='t_description'>"+t_description+"</div >"+
            "<div class='liItem-del'><a title='删除' onclick='del_protocol_list(this)' class='labelItem label label-danger'><i class='fa fa-trash-o'></i></a></div>"+
            "</li>"
        )
      }

      function del_protocol_list(obj){
         $(obj).parent('div').parent('li').remove();
      }

      function protocol_edit(){   //用户协议修改
        ids=[],protocolDelArr=[];
        $('#protocol-content').addClass('mb30');
        $('#footerEditprotocol').css('display','flex');
        $('.protocol-list .item-content #radioBtn').css('display','flex');
        $('[type="radio"]').get(0).checked=false;
        $('[type="radio"]').removeAttr('checked');
        $('[type="checkbox"]').get(0).checked=false;
        $('[type="checkbox"]').removeAttr("checked");      //未选状态
        $('#footerDelprotocol').css('display','none');     //编辑时删除不显示
        $('.protocol-list .item-content #checkboxBtn').css('display','none');  //编辑时多选不显示
      }

      function protocol_del(){    //用户私协删除
        ids=[],protocolDelArr=[];
        $('#protocol-content').addClass('mb30');
        $('#footerDelprotocol').css('display','flex');
        $('.protocol-list .item-content #checkboxBtn').css('display','flex');
        $('[type="radio"]').get(0).checked=false;
        $('[type="radio"]').removeAttr('checked');
        $('[type="checkbox"]').get(0).checked=false;
        $('[type="checkbox"]').removeAttr("checked");     //未选状态
        $('#footerEditprotocol').css('display','none');   //删除时编辑不显示
        $('.protocol-list .item-content #radioBtn').css('display','none');  //删除时单选不显示
      }

      function allProtocol_check(obj){
          if($(obj).get(0).checked == true){
            ids=[];
            $('[type="checkbox"]').prop('checked',true); 
            $('.protocol-list li').each(function(){
              ids.push($(this).find('#itemId').text());
            })         
          }else{
            $('[type="checkbox"]').prop('checked',false);
            ids=[];
          }
      }

      function ajax_statistics(){
        $.ajax({
            url:'/statistics/getStatistics',
            type:'post',
            dataType:'json',
            success:function(data){
                $('.h5statistical-number').empty();
                statistical_li(data);
            },
                statusCode : {
                    503: function(xhr, ajaxOptions, thrownError) {
                        $.toast('数据加载超时');
                    }
                }
        })
      }
      function statistical_li(data){
          $('.h5statistical-number').setStatisticsPhone(4,
            [
                {'color':'green','icon':'fa-desktop','text':'设备','value':data.data.devicenum,'id':'sysdevice','url':'/h5/device/list','title':'设备管理'},
                {'color':'blue','icon':'fa-clipboard','text':'业务','value':data.data.businessnum,'id':'sysbusiness','url':'/h5/business/list','title':'业务管理'},
                {'color':'brown','icon':'fa-file-o','text':'私有协议','value':data.data.ktvgroupnum,'id':'sysprotocol','url':'/h5/protocol/list','title':'私有协议'},
                {'color':'red','icon':'fa-th-large','text':'应用','value':data.data.appnum,'id':'sysapp','url':'/h5/app/list','title':'应用管理'}
            ])
      }

      var metadataktv='';
     var privatedataktv='';
      function bbb(obj){
        metadataktv =  JSON.parse($(obj).find('#headdevicedata').text()).metadata;
        privatedataktv = JSON.parse($(obj).find('#headdevicedata').text()).privatedata;
        console.log(privatedataktv.length);
        if(privatedataktv.length!=0){
           console.log(666666);
          $(obj).attr('href','/h5/devices/detailsPage');
        }else{
          $(obj).attr('href','#');
        }
                 
       $.ajax({
              url:'/h5/devices/detailsPage',
              type:'post',
              data:{'metadata':metadataktv,'privatedata':JSON.stringify(privatedataktv)},
              dataType:'json',
              success:function(data){
             
                },
                statusCode : {
                    503: function(xhr, ajaxOptions, thrownError) {
                        $.toast('数据加载超时');
                    }
                }
            });
      }

      function type_onversion(t){
          switch (t)
          {
          case 0:
            return "字节";
          case 1:
            return "整形";
          case 2:
            return "浮点型";
          case 3:
           return "字符串";
          }
      }
